home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 031a / adg_4_6.zip / METER.C < prev    next >
C/C++ Source or Header  |  1991-02-21  |  6KB  |  178 lines

  1. /****************************************************************************
  2. Module name: Meter.C
  3. Programmer : Jeffrey M. Richter.
  4. *****************************************************************************/
  5.  
  6. #include "..\nowindws.h"
  7. #define  OEMRESOURCE
  8. #undef   NOCOLOR
  9. #undef   NOCTLMGR
  10. #undef   NODRAWTEXT
  11. #undef   NOGDI
  12. #undef   NOKERNEL
  13. #undef   NOLSTRING
  14. #undef   NOMEMMGR
  15. #undef   NORASTEROPS
  16. #undef   NOUSER
  17. #undef   NOVIRTUALKEYCODES
  18. #undef   NOWINMESSAGES
  19. #undef   NOWINOFFSETS
  20. #undef   NOWINSTYLES
  21. #include <windows.h>
  22.  
  23. #include "meter.h"
  24.  
  25. HANDLE _hInstance = NULL;
  26. char _szControlName[] = "Meter";
  27.  
  28. #define CBWNDEXTRA         (4)
  29. #define GWW_PARTSINJOB     (0)
  30. #define GWW_PARTSCOMPLETE  (2)
  31.  
  32. static short NEAR PASCAL RegisterControlClass (HANDLE hInstance);
  33. LONG FAR PASCAL MeterWndFn (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam);
  34.  
  35. /********* Window's Dynamic-Link Library Initialization Routines ***********/
  36.  
  37. BOOL FAR PASCAL LibMain (HANDLE hModule, WORD wDataSeg, WORD wHeapSize, LPSTR lpszCmdLine) {
  38.    BOOL fOk;
  39.    _hInstance = hModule;
  40.    if (wHeapSize != 0) UnlockData(0);  // Let data segment move
  41.    fOk = RegisterControlClass(hModule);
  42.    return(fOk);   // return TRUE if initialization is successful
  43. }
  44.  
  45. int FAR PASCAL WEP (int nSystemExit) {
  46.    switch (nSystemExit) {
  47.       case WEP_SYSTEM_EXIT:   // System is shutting down.
  48.          break;
  49.  
  50.       case WEP_FREE_DLL:      // Usage count is zero (0).
  51.          break;
  52.    }
  53.    UnregisterClass(_szControlName, _hInstance);
  54.    return(1);                 // WEP function successful.
  55. }
  56.  
  57. static short NEAR PASCAL RegisterControlClass (HANDLE hInstance) {
  58.    WNDCLASS wc;
  59.    wc.style         = CS_GLOBALCLASS | CS_HREDRAW | CS_VREDRAW;
  60.    wc.lpfnWndProc   = MeterWndFn;
  61.    wc.cbClsExtra    = 0;
  62.    wc.cbWndExtra    = CBWNDEXTRA;
  63.    wc.hInstance     = hInstance;
  64.    wc.hIcon         = NULL;
  65.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  66.    wc.hbrBackground = NULL;
  67.    wc.lpszMenuName  = NULL;
  68.    wc.lpszClassName = _szControlName;
  69.    return(RegisterClass(&wc));
  70. }
  71.  
  72. LONG FAR PASCAL MeterWndFn (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam) {
  73.    LONG lResult = 0;
  74.    char szPercentage[10];
  75.    RECT rcClient, rcPrcnt;
  76.    PAINTSTRUCT ps;
  77.    WORD wPartsInJob, wPartsComplete;
  78.    HBRUSH hBrush;
  79.    DWORD dwColor;
  80.  
  81.    switch (wMsg) {
  82.       case WM_GETDLGCODE:
  83.          lResult = DLGC_STATIC;
  84.          break;
  85.  
  86.       case WM_CREATE:   // lParam == &CreateStruct
  87.          SendMessage(hWnd, MM_SETPARTSINJOB, 100, 0l);
  88.          SendMessage(hWnd, MM_SETPARTSCOMPLETE, 50, 0);
  89.          break;
  90.  
  91.       case WM_PAINT:
  92.          wPartsInJob = (WORD) SendMessage(hWnd, MM_GETPARTSINJOB,  0, 0l);
  93.          wPartsComplete = (WORD) SendMessage(hWnd, MM_GETPARTSCOMPLETE, 0, 0l);
  94.          if (wPartsInJob == 0) {
  95.             wPartsInJob = 1;
  96.             wPartsComplete = 0;
  97.          }
  98.          wsprintf(szPercentage, "%d%%", (100 * wPartsComplete) / wPartsInJob);
  99.  
  100.          BeginPaint(hWnd, &ps);
  101.  
  102.          // Set-up default foreground and background text colors.
  103.          SetBkColor(ps.hdc, GetSysColor(COLOR_WINDOW));
  104.          SetTextColor(ps.hdc, GetSysColor(COLOR_WINDOWTEXT));
  105.  
  106.          // Send WM_CTLCOLOR message to parent in case parent want to 
  107.          // use a different color in the Meter control.
  108.          hBrush = (HBRUSH) SendMessage(GetParent(hWnd), WM_CTLCOLOR, ps.hdc,
  109.             MAKELONG(hWnd, CTLCOLOR_METER));
  110.  
  111.          // Always use brush returned by parent.
  112.          SelectObject(ps.hdc, hBrush);
  113.  
  114.          SetTextAlign(ps.hdc, TA_CENTER | TA_TOP);
  115.  
  116.          // Invert the foreground and background colors.
  117.          dwColor = GetBkColor(ps.hdc);
  118.          SetBkColor(ps.hdc, SetTextColor(ps.hdc, dwColor));
  119.  
  120.          // Set rectangle coordinates to include only left
  121.          // percentage of the window.
  122.          GetClientRect(hWnd, &rcClient);
  123.          SetRect(&rcPrcnt, 0, 0,
  124.             (rcClient.right * wPartsComplete) / wPartsInJob, rcClient.bottom);
  125.  
  126.          // Output the percentage value in the window.
  127.          // Function also paints left part of window.
  128.          ExtTextOut(ps.hdc, rcClient.right / 2,
  129.             (rcClient.bottom - HIWORD(GetTextExtent(ps.hdc, "X", 1))) / 2,
  130.             ETO_OPAQUE | ETO_CLIPPED, &rcPrcnt,
  131.             szPercentage, lstrlen(szPercentage), NULL);
  132.  
  133.          // Adjust rectangle so that it includes the remaining 
  134.          // percentage of the window.
  135.          rcPrcnt.left = rcPrcnt.right;
  136.          rcPrcnt.right = rcClient.right;
  137.  
  138.          // Invert the foreground and background colors.
  139.          dwColor = GetBkColor(ps.hdc);
  140.          SetBkColor(ps.hdc, SetTextColor(ps.hdc, dwColor));
  141.  
  142.          // Output the percentage value a second time in the window.
  143.          // Function also paints right part of window.
  144.          ExtTextOut(ps.hdc, rcClient.right / 2,
  145.             (rcClient.bottom - HIWORD(GetTextExtent(ps.hdc, "X", 1))) / 2,
  146.             ETO_OPAQUE | ETO_CLIPPED, &rcPrcnt,
  147.             szPercentage, lstrlen(szPercentage), NULL);
  148.  
  149.          EndPaint(hWnd, &ps);
  150.          break;
  151.  
  152.       case MM_SETPARTSINJOB:
  153.          SetWindowWord(hWnd, GWW_PARTSINJOB, wParam);
  154.          InvalidateRect(hWnd, NULL, FALSE);
  155.          UpdateWindow(hWnd);
  156.          break;
  157.  
  158.       case MM_GETPARTSINJOB:
  159.          lResult = (LONG) GetWindowWord(hWnd, GWW_PARTSINJOB);
  160.          break;
  161.  
  162.       case MM_SETPARTSCOMPLETE:
  163.          SetWindowWord(hWnd, GWW_PARTSCOMPLETE, wParam);
  164.          InvalidateRect(hWnd, NULL, FALSE);
  165.          UpdateWindow(hWnd);
  166.          break;
  167.  
  168.       case MM_GETPARTSCOMPLETE:
  169.          lResult = (LONG) GetWindowWord(hWnd, GWW_PARTSCOMPLETE);
  170.          break;
  171.  
  172.       default:
  173.          lResult = DefWindowProc(hWnd, wMsg, wParam, lParam);
  174.          break;
  175.    }
  176.    return(lResult);
  177. }
  178.